package org.xin.framework.controller;


import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.xin.common.result.Result;
import org.xin.common.util.XinUtil;
import org.xin.framework.base.controller.IBaseController;
import org.xin.framework.pojo.entity.SysDictionary;
import org.xin.framework.service.ISysDictionaryItemService;
import org.xin.framework.service.ISysDictionaryService;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * <p>
 * 系统模块-字典表 前端控制器
 * </p>
 *
 * @author: xin
 * @date: Create in 2021-09-29
 */

@Api(tags = "系统模块-字典")
@CrossOrigin
@RestController
@RequestMapping("/sysDictionary")
public class SysDictionaryController extends IBaseController<SysDictionary, ISysDictionaryService> {

    @Autowired
    private ISysDictionaryItemService dictionaryItemService;

    @Override
    public Result superDelete(@RequestBody List<String> ids){
        AtomicBoolean hasData = new AtomicBoolean(false);
        List<String> removeIds = new ArrayList<>(){{
            ids.forEach(id->{
                if (XinUtil.isNull(dictionaryItemService.selectByDictionaryId(id))) {
                    this.add(id);
                } else if (!hasData.get()){
                    hasData.set(true);
                }
            });
        }};
        service.removeByIds(removeIds);
        if (hasData.get()) {
            return Result.ok("提示：部分数据被其它数据使用!");
        }
        return Result.ok();
    }
}
